<?php
$main_page_header = 'location:../registration.html';
if (empty($_POST['login']) or empty($_POST['password1']) or empty($_POST['password2']))
{
	header($main_page_header);
	exit;
}
$login = $_POST['login'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
$reg_login = '/^[A-Za-z0-9_]{2,32}$/';
$reg_pwd = '/^[A-Za-z0-9_!@#$%^&*()-+=;:\]\[<>?.,]{6,32}$/';
if (!preg_match($reg_login, $login))
{
	header($main_page_header);
	exit;
}

if (!preg_match($reg_pwd, $password1))
{
	header($main_page_header);
	exit;
}

if ($password1 != $password2)
{
	header($main_page_header);
	exit;
}

mysql_connect('localhost', 'root');
mysql_select_db('dots');

$hash_pwd = sha1($login.'&'.$password1);

$query = "SELECT userID FROM users WHERE login='$login'";
$result = mysql_query($query);
if (!$result)
{
	header($main_page_header);
	exit;
}

$rows = mysql_fetch_row($result);
if ($rows)
{
	header($main_page_header);
	exit;
}

$query = "INSERT INTO users (login, password) VALUES ('$login', '$hash_pwd')";
$result = mysql_query($query);
if(!$result)
{
	header($main_page_header);
	exit;
}

header('location:../index.php');
?>
